package com.rightsidetech.http.controller.admin.mall;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.mall.dto.request.SpecsPageReq;
import com.rightsidetech.mall.dto.request.SpecsReq;
import com.rightsidetech.mall.dto.request.SpecsUpReq;
import com.rightsidetech.mall.dto.response.SpecsPageResp;
import com.rightsidetech.mall.service.MallSpecsService;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/mall_specs/")
@ShenyuSpringMvcClient(path = "/admin/mall_specs/**")
@Api(tags = "Admin: 商品规格相关")
public class MallSpecsController extends BaseController {

    @Resource
    private MallSpecsService mallSpecsService;

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/save")
    @ApiOperation("添加规格名和规格值")
    public ResponseData saveSpecs(@RequestBody SpecsReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(null == loginInfoId || null == loginName){
            return ResponseData.fail("未找到登录人");
        }
        return mallSpecsService.saveSpecs(req,loginName,loginInfoId);
    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("分页查询规格")
    public ResponseData<PageInfo<SpecsPageResp>> pageSpecs(SpecsPageReq req){
         return mallSpecsService.page(req);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/del")
    @ApiOperation("删除规格")
    public ResponseData deleteSpecs(@RequestBody DelReq req){
        return mallSpecsService.isDelete(req);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/update")
    @ApiOperation("修改规格")
    public ResponseData updateSpecs(@RequestBody SpecsUpReq req) throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(null == loginInfoId || null == loginName){
            return ResponseData.fail("未找到登录人");
        }
        return mallSpecsService.updateSpecs(req,loginName,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/get_values")
    @ApiOperation("获取规格下面的规格值")
    public ResponseData<List<SpecsPageResp>> getValueList(@RequestParam(value = "id",required = true) Long id){
         return mallSpecsService.getSpecsValue(id);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/all")
    @ApiOperation("获取所有规格")
    public ResponseData<List<SpecsPageResp>> getAll(){
         return mallSpecsService.getAll();
    }
}
